﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Data.Entity;
using AppBox.BLL;
using FineUI;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Box;
using ZHDJ.WebPage;
using Menu = ZHDJ.Data.Models.Box.Menu;
using PageBase = AppBox.BLL.PageBase;


namespace AppBox.admin
{
    public partial class menu_new : BLL.PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "CoreMenuNew";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();

            //// 模块名称列表
            //ddlModules.DataSource = ModuleTypeHelper.GetAppModules();
            //ddlModules.DataBind();

            //ddlModules.SelectedValue = ModuleTypeHelper.Module2String(ModuleType.None);

            BindDDL();

            InitIconList(iconList);
        }

        public void InitIconList(FineUI.RadioButtonList iconList)
        {
            string[] icons = new string[] { "tag_yellow", "tag_red", "tag_purple", "tag_pink", "tag_orange", "tag_green", "tag_blue" };
            foreach (string icon in icons)
            {
                string value = String.Format("~/Content/icon/{0}.png", icon);
                string text = String.Format("<img style=\"vertical-align:bottom;\" src=\"{0}\" />&nbsp;{1}", ResolveUrl(value), icon);

                iconList.Items.Add(new RadioItem(text, value));
            }
        }

        private void BindDDL()
        {
            List<Menu> menus = ResolveDDL<Menu>(MenuHelper.Menus);

            // 绑定到下拉列表（启用模拟树功能）
            ddlParent.EnableSimulateTree = true;
            ddlParent.DataTextField = "Name";
            ddlParent.DataValueField = "ID";
            ddlParent.DataSimulateTreeLevelField = "TreeLevel";
            ddlParent.DataSource = menus;
            ddlParent.DataBind();

            // 选中根节点
            ddlParent.SelectedValue = "0";
        }

        #endregion

        #region Events

        private void SaveItem()
        {
            Menu item = new Menu();
            item.ID = StringHelper.NewID;
            item.Name = tbxName.Text.Trim();
            item.NavigateUrl = tbxUrl.Text.Trim();
            item.SortIndex = Convert.ToInt32(tbxSortIndex.Text.Trim());
            item.Remark = tbxRemark.Text.Trim();

            string parentID = Convert.ToString(ddlParent.SelectedValue);
            if (parentID == "-1")
            {
                item.Parent = null;
            }
            else
            {
                Menu menu = Attach<Menu>(parentID);
                item.Parent = menu;
            }

            string viewPowerName = tbxViewPower.Text.Trim();
            var groupName = viewPowerName.Replace("View", "");
            if (tbxViewPower.Text.Trim()!="" && tbxUrl.Text.Trim()!="" && DB.Powers.Count(a => a.GroupName== groupName)<1)
            {
              
                DB.Powers.AddRange(new List<Power>()
                {
                    new Power(){ID = StringHelper.NewID,GroupName = groupName,Name = viewPowerName,Title = item.Name+"(浏览)"},
                    new Power(){ID = StringHelper.NewID,GroupName = groupName,Name = groupName + "Add",Title = item.Name+"(增加)"},
                    new Power(){ID = StringHelper.NewID,GroupName = groupName,Name = groupName + "Delete",Title = item.Name+"(删除)"},
                    new Power(){ID = StringHelper.NewID,GroupName = groupName,Name = groupName + "Edit",Title = item.Name+"(修改)"}

                });
            }
            DB.SaveChanges();
            item.ViewPower = DB.Powers.Where(p => p.Name == viewPowerName).FirstOrDefault();

            DB.Menus.Add(item);
            DB.SaveChanges();

        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveItem();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        #endregion

    }
}
